
import TreeTraversal.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.LinkedList;
public class rightSideView {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode a = queue.poll();
                if(i == size - 1){
                    result.add(a.val);
                }
                if(a.left != null){
                    queue.offer(a.left);
                }
                if(a.right != null){
                    queue.offer(a.right);
                }
            }
        }
        return result;
    }
}
